import pykeyboard
import pymouse
import time
import os
import random

class MonkeySimple():
    # 初始化鼠标对象和键盘对象
    def __init__(self):
        self.mouse = pymouse.PyMouse()
        self.keyboard = pykeyboard.PyKeyboard()

    #获取随机坐标
    def rand_pos(self):
        x = random.randint(0, 1600)
        y = random.randint(0, 900)
        return x, y

    #执行随机坐标左键单击
    def rand_click(self):
        x, y = self.rand_pos()
        self.mouse.move(x, y)
        self.mouse.click(x, y, button=1, n=1)
        time.sleep(2)
        print('在坐标[%d, %d]处执行单击操作'%(x, y))

    #执行左键双击操作
    def rand_double_click(self):
        x, y = self.rand_pos()
        self.mouse.move(x, y)
        self.mouse.click(x, y, button=1, n=2)
        time.sleep(2)
        print('在坐标[%d, %d]处执行双击操作'%(x, y))

    #执行右键单击
    def rand_right_click(self):
        x, y = self.rand_pos()
        self.mouse.move(x, y)
        self.mouse.click(x, y, button=2, n=1)
        time.sleep(2)
        print('在坐标[%d, %d]处执行右键单击操作'%(x, y))

    #执行下拉框选择
    def rand_select(self, count):
        x, y = self.rand_pos()
        self.mouse.move(x, y)
        self.mouse.click(x, y)
        for i in range(count):
            self.keyboard.press_key(self.keyboard.down_key)
            self.keyboard.release_key(self.keyboard.down_key)
        self.keyboard.press_key(self.keyboard.enter_key)
        self.keyboard.release_key(self.keyboard.enter_key)
        time.sleep(2)
        print('在坐标[%d, %d]处执行下拉框操作'%(x, y))

    #执行随机字符串输入
    def rand_input(self):
        x, y = self.rand_pos()
        self.mouse.move(x, y)
        self.mouse.click(x, y)
        str_list = ['hello world', '蜗牛学院', '你瞅啥', '瞅你咋滴', '再瞅一个试试', '试试就试试', 'OK', 2345]
        rand_str = random.choice(str_list)
        self.keyboard.type_string(rand_str)
        time.sleep(2)
        print('在坐标[%d, %d]处执行字符串%s输入操作'%(x, y, rand_str))

    #执行随机按键
    def rand_key(self):
        x, y = self.rand_pos()
        self.mouse.move(x, y)
        key_list = [self.keyboard.alt_key, self.keyboard.enter_key, self.keyboard.up_key, self.keyboard.down_key, self.keyboard.cancel_key]
        rand_key = random.choice(key_list)
        self.keyboard.press_key(rand_key)
        self.keyboard.release_key(rand_key)
        time.sleep(2)
        print('在坐标[%d, %d]处执行按键%s操作'%(x, y, rand_key))

    #定义随机规则
    def start(self, count, n=2):
        for i in range(count):
            seed = random.randint(0, 100)
            if seed < 20:
                self.rand_click()
            elif seed < 40:
                self.rand_double_click()
            elif seed < 60:
                self.rand_right_click()
            elif seed < 80:
                self.rand_select(n)
            elif seed < 90:
                self.rand_input()
            else:
                self.rand_key()


if __name__ == '__main__':
    # os.system('start /b ' + r'C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe')
    os.startfile("C:\Program Files\Google\Chrome\Application\chrome.exe")
    time.sleep(2)

    monkey = MonkeySimple()
    monkey.start(10)
    print("测试结束.")
    os.system("taskkill -F -IM chrome.exe")


